package com.grm.workflow.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.grm.common.exception.BusinessException;
import com.grm.workflow.mapper.WfDeployFormMapper;
import com.grm.workflow.mapper.WfFormMapper;
import com.grm.workflow.model.WfDeployForm;
import com.grm.workflow.model.WfForm;
import com.grm.workflow.service.WfDeployFormService;
import com.grm.workflow.utils.BpmnModelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.bpmn.model.UserTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class WfDeployFormServiceImpl implements WfDeployFormService {
    @Autowired
    private WfDeployFormMapper wfDeployFormMapper;
    @Autowired
    private WfFormMapper wfFormMapper;

    @Override
    public void add(String deployId, BpmnModel bpmnModel) {
        List<WfDeployForm> deployFormList = new ArrayList<>();
        // 获取开始节点
        StartEvent startEvent = BpmnModelUtils.getStartEvent(bpmnModel);
        if (ObjectUtil.isNull(startEvent)) {
            throw new BusinessException("开始节点不存在，请检查流程设计是否有误！");
        }
        // 保存开始节点表单信息
        WfDeployForm startDeployForm = buildDeployForm(deployId, startEvent);
        if (ObjectUtil.isNotNull(startDeployForm)) {
            deployFormList.add(startDeployForm);
        }
        // 保存用户节点表单信息
        // todo 只保存提交表单信息，暂时不涉及用户节点的表单
//        Collection<UserTask> userTasks = BpmnModelUtils.getAllUserTaskEvent(bpmnModel);
//        if (CollUtil.isNotEmpty(userTasks)) {
//            for (UserTask userTask : userTasks) {
//                WfDeployForm userTaskDeployForm = buildDeployForm(deployId, userTask);
//                if (ObjectUtil.isNotNull(userTaskDeployForm)) {
//                    deployFormList.add(userTaskDeployForm);
//                }
//            }
//        }
        // 批量新增部署流程和表单关联信息
        wfDeployFormMapper.batchInsert(deployFormList);
    }

    /**
     * 构建部署表单关联信息对象
     * @param deployId 部署ID
     * @param node 节点信息
     * @return 部署表单关联对象。若无表单信息（formKey），则返回null
     */
    private WfDeployForm buildDeployForm(String deployId, FlowNode node) {
        String formKey = BpmnModelUtils.getFormKey(node);
        if (StringUtils.isEmpty(formKey)) {
            return null;
        }
        Long formId = Long.parseLong(formKey);
        WfForm wfForm = wfFormMapper.queryById(formId);
        if (ObjectUtil.isNull(wfForm)) {
            throw new BusinessException("表单信息查询错误");
        }
        WfDeployForm wfDeployForm = new WfDeployForm();
        wfDeployForm.setDeployId(deployId);
        wfDeployForm.setFormId(formId);
        wfDeployForm.setNodeId(node.getId());
        wfDeployForm.setNodeName(node.getName());
        return wfDeployForm;
    }
}
